package at.ac.thornwishstalon.ocrapp.main;

import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.MediaStore;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import at.ac.thornwishstalon.ocrapp.R;

public class OcrappActionProvider extends ActionProvider{ //implements OnMenuItemClickListener {
	private final String TAG = "ResourceActionProvider";	
	private Context context;
	private Intent intent;
	private int flags= PackageManager.MATCH_DEFAULT_ONLY;
	private Intent camIntent;

	public OcrappActionProvider(Context context) {
		super(context);

		this.context=context;


		camIntent= new Intent();
		camIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
		
		intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);

	}

	@Override
	public View onCreateActionView() {
		LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.main_fragment_image_action,null);
        return view;
	}

	@Override
	public boolean hasSubMenu() {
		return true;
	}


	@Override
	public void onPrepareSubMenu(SubMenu subMenu) {
		Log.d(TAG, "onPrepareSubMenu");
		subMenu.clear();

		PackageManager manager= context.getPackageManager();

		List<ResolveInfo> list= manager.queryIntentActivities(camIntent, flags);

		for(int i=0;i< list.size();i++){
			ActivityInfo info=list.get(i).activityInfo;
			subMenu.add(0, i, i, "")
			.setIcon(info.loadIcon(manager))
			//.setOnMenuItemClickListener(this)
			.setIntent(new Intent(info.name));
		}

		list= manager.queryIntentActivities(intent, flags);

		for(int i=0;i< list.size();i++){
			ActivityInfo info=list.get(i).activityInfo;
			subMenu.add(0, i, i, "")
			.setIcon(info.loadIcon(manager));
			//.setOnMenuItemClickListener(this)
			//.setIntent(new Intent(info.applicationInfo.));
		}
	}
	
	
	public boolean onMenuItemClick(MenuItem item) {
	
		return true;
	}
	
	
	





}
